<template>
	<view>
		<u-navbar titleStyle="color:#fff" leftIconColor="#fff" bgColor="transparent" :autoBack="true"
			:placeholder="false">
		</u-navbar>
		<view class="login-content">
			<view style="height: 9vh;"></view>
			<view class="login-head">
				<view class="head-title">
					注册账号
				</view>
				<view class="head-text">
					欢迎您的注册，探索未来
				</view>
			</view>
			<view style="height: 5vh;"></view>
			<view class="entry">
				<view class="content-title">
					手机号
				</view>
				<u--input maxlength="11" border="none" color="#fff" v-model="tel" placeholder="请输入您的手机号"></u--input>
			</view>
			<view class="entry">
				<view class="content-title">
					登录密码
				</view>
				<u--input maxlength="16" :type="pass_type" border="none" color="#fff" v-model="password"
					placeholder="8~16位数字字母组合">
					<template slot="suffix">
						<u-icon v-if="pass_type == 'password'" name="eye-fill" color="#fff" size="18"
							@click="changeType"></u-icon>
						<u-icon v-else name="eye-off" color="#fff" size="18" @click="changeType"></u-icon>
					</template>
				</u--input>
			</view>
			<view class="entry">
				<view class="content-title">
					用户名
				</view>
				<u--input maxlength="16" border="none" color="#fff" v-model="account" placeholder="请设置您的用户名"></u--input>
			</view>
			<view class="entry">
				<view class="content-title">
					交易密码
				</view>
				<u--input :type="pay_word" maxlength="6" border="none" color="#fff" v-model="pay_password"
					placeholder="6位纯数字">
					<template slot="suffix">
						<u-icon v-if="pay_word == 'password'" name="eye-fill" color="#fff" size="18"
							@click="hadleType"></u-icon>
						<u-icon v-else name="eye-off" color="#fff" size="18" @click="hadleType"></u-icon>
					</template>
				</u--input>
			</view>
			<view class="entry">
				<view class="content-title">
					验证码
				</view>
				<u--input type="number" maxlength="6" border="none" color="#fff" v-model="code" placeholder="请输入验证码">
					<template slot="suffix">
						<u-code ref="uCode" @change="codeChange" seconds="60" changeText="X秒重新获取"></u-code>
						<view @tap="getCode" style="color: #98EE84;">
							{{tips}}
						</view>
					</template>
				</u--input>
			</view>
			<view class="entry">
				<view class="content-title">
					邀请码 
				</view>
				<u--input type="text" maxlength="10" border="none" color="#fff" v-model="yqcode" placeholder="请填写邀请码">
				</u--input>
			</view>
			<view class="btn" @click="goRegister">
				<u-button size="large" color="linear-gradient( 90deg, #C8F673 0%, #98F083 100%)" type="primary"
					text="注册" shape="circle" class="btn-color"></u-button>
				<image class="logo" src="../../static/login/login-logo.png" mode=""></image>
			</view>
		</view>
	</view>
</template>

<script>
	import MD5 from '@/common/md5.min.js'
	export default {
		data() {
			return {
				account: '',
				tel: '',
				password: '',
				pay_password: '',
				code: '',
				yqcode: '',
				pass_type: 'password',
				pay_word: 'password',
				tips: '',
			}
		},
		onLoad(e) {
			if (e.code) {
				this.yqcode = e.code
			}
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.tel == '') {
					uni.showToast({
						title: '请输入您的手机号！',
						icon: 'none'
					})
					return
				}
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					let params = {
						phone: this.tel,
					}
					this.$Request({
						method: 'POST',
						url: '/api/login/sendRegister',
						data: params
					}).then(res => {
						if (res.code == 1) {
							uni.hideLoading();
							// 这里此提示会被this.start()方法中的提示覆盖
							uni.$u.toast('验证码已发送');
							// 通知验证码组件内部开始倒计时
							this.$refs.uCode.start();
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					})
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			change(e) {
				console.log('change', e);
			},
			changeType() {
				if (this.pass_type == 'password') {
					this.pass_type = 'text'
				} else {
					this.pass_type = 'password'
				}
			},
			hadleType() {
				if (this.pay_word == 'password') {
					this.pay_word = 'text'
				} else {
					this.pay_word = 'password'
				}
			},
			goRegister() {
				if (this.tel == '') {
					uni.showToast({
						title: '手机号不能为空！',
						icon: 'none'
					})
					return
				}
				if (this.code == '') {
					uni.showToast({
						title: '验证码不能位空',
						icon: 'none'
					})
					return
				}
				if (this.password == '') {
					uni.showToast({
						title: '登录密码不能位空',
						icon: 'none'
					})
					return
				}
				if (this.password.length < 8) {
					uni.showToast({
						title: '密码小于8位数',
						icon: 'none'
					})
					return
				}
				if (this.account == '') {
					uni.showToast({
						title: '用户名不能位空',
						icon: 'none'
					})
					return
				}
				if (this.pay_password == '') {
					uni.showToast({
						title: '交易密码不能位空',
						icon: 'none'
					})
					return
				}
				if (this.pay_password.length != 6) {
					uni.showToast({
						title: '交易密码必须为6位数',
						icon: 'none'
					})
					return
				}
				if (this.yqcode == '') {
					uni.showToast({
						title: '请填写邀请码！',
						icon: 'none'
					})
					return
				}
				const regex = /^[1-9]\d*$/;
				if (regex.test(this.pay_password) == false) {
					uni.showToast({
						title: '支付密码只能为纯数字！',
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					mask: true
				})
				let params = {
					phone: this.tel,
					password: MD5(this.password),
					pay_pass: this.pay_password,
					user: this.account,
					code: this.code,
					guid: this.yqcode
				}
				this.$Request({
					method: 'POST',
					url: '/api/Login/register',
					data: params
				}).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						uni.showToast({
							title: '注册成功！',
							icon: 'none'
						})
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/login/index'
							})
						}, 1000)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>
<style>
	.btn-color {
		color: #17191A !important;
		font-weight: bold;
	}
</style>
<style lang="scss" scoped>
	.login-content {
		width: 100%;
		height: 100vh;
		background-image: url('../../static/login/login-bg.png');
		background-repeat: no-repeat;
		background-size: 100% 100vh;
		padding: 30rpx;
		color: #fff;

		.login-head {
			.head-title {
				font-size: 55rpx;
				font-weight: 600;
			}

			.head-text {
				font-size: 28rpx;
				margin-top: 10rpx;
			}
		}

		.entry {
			padding: 30rpx;
			background-color: rgba(238, 238, 238, 0.2);
			border-radius: 50rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 28rpx;
			margin-top: 30rpx;

			.content-title {
				width: 150rpx;
				font-size: 28rpx;
			}
		}

		.btn {
			margin-top: 80rpx;
			position: relative;

			.logo {
				width: 126rpx;
				height: 133rpx;
				position: absolute;
				left: 40rpx;
				top: -45rpx;
			}
		}

	}
</style>